#include "modbusmanger.h"

ModbusManger::ModbusManger(QObject *parent) : QObject(parent)
{
    qRegisterMetaType<ModbusRequest>("ModbusRequest");
}
void ModbusManger::register_dev_client(ModbusClient_dev *dev, ModbusClient *client)
{
    dev_client_map[dev->getDeviceId()] = dev;
    dev_client_client_map[dev->getDeviceId()] = client;
    dev->setModbusClient(client);
    connect(dev, &ModbusClient_dev::read_register, client, &ModbusClient::addRequest);
    connect(client, &ModbusClient::request_receive_data, this, &ModbusManger::request_receive_data,Qt::UniqueConnection);
}
void ModbusManger::unregister_dev(ModbusClient_dev *dev)
{
    dev_client_map.remove(dev->getDeviceId());
    disconnect(dev, &ModbusClient_dev::read_register, dev_client_client_map[dev->getDeviceId()], &ModbusClient::addRequest);
    dev_client_client_map.remove(dev->getDeviceId());
}
void ModbusManger::unregister_client(ModbusClient *client)
{
    disconnect(client, &ModbusClient::request_receive_data, this, &ModbusManger::request_receive_data);

}
void ModbusManger::request_receive_data(ModbusRequest request)
{
    static int i=0 ;
    i+=1;
    if(dev_client_map.contains(request.device_id))
    {
        dev_client_map[request.device_id]->reciveRequest(request);
    }
}
